package cn.diancan.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.diancan.dao.MyBaseDao;
import cn.diancan.exception.OutMsgException;
import cn.diancan.model.SysManager;
import cn.diancan.model.types.CommonType;
import cn.diancan.service.SysManagerService;
import cn.diancan.util.MD5;


@Component("sysManagerService")
public class SysManagerServiceImpl  implements SysManagerService {

    @Resource(name="MyBaseDao")
    private MyBaseDao<SysManager> sysManagerDao;
    
    
    @Override
    public SysManager findLogined(String managerName, String psw)throws OutMsgException {
	SysManager manager = sysManagerDao.get("FROM SysManager where userName=? and passWd=? and isDelete=?",
		new Object[] { managerName, MD5.createPassword(psw), CommonType.Delete.NO });
	if (null == manager) {
	    throw new OutMsgException("该用户不存在");
	}
	try {
	    // SmsUtil.sendSMS("您的管理员账号被登录", DateUtil.currentDateTime(),
	    // manager.getPhoneNum());
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return manager;
    }

    @Override
    public SysManager findByManagerName(String managerName)
	    throws OutMsgException {
	if ("".equals(managerName.trim())) {
	    throw new OutMsgException("参数错误");
	}
	return sysManagerDao.get("FROM SysManager where userName=?",
		new Object[] { managerName });
    }

    @Override
    public SysManager findByManagerId(Long managerId) throws OutMsgException {
	if (null == managerId) {
	    throw new OutMsgException("参数错误");
	}
	SysManager bean = sysManagerDao.get("From SysManager where id=?",
		new Object[] { managerId });
	if (null == bean) {
	    throw new OutMsgException("该用户不存在");
	}
	return bean;
    }

    @Override
    public boolean saveMangerAccount(SysManager manager) throws OutMsgException {
	SysManager ymManager = findByManagerName(manager.getUserName());
	if (null == ymManager) {
	    sysManagerDao.save(manager);
	} else {
	    throw new OutMsgException("该用户已存在");
	}
	return true;
    }

    @Override
    public List<SysManager> findByAllManager() {
	return sysManagerDao.find("From SysManager WHERE isDelete=?",
		new Object[] { CommonType.Delete.NO });
    }

    @Override
    public boolean updateManager(SysManager manager) throws OutMsgException {
	if (null != findByManagerId(manager.getId())) {
	    sysManagerDao.update(manager);
	}
	return true;
    }

    @Override
    public boolean updateIsDelete(Long id, int is) {
	SysManager bean;
	try {
	    bean = findByManagerId(id);
	    if (!(is == 0 || is == 1)) {
		return false;
	    }
	    if (bean.getIsDelete() == is) {
		return true;
	    }
	    String hql2 = " SET isDelete=? WHERE id=?";
	    sysManagerDao.executeHql("UPDATE SysManager " + hql2, new Object[] {
		    is, bean.getId() });
	    return true;
	} catch (OutMsgException e) {
	    e.printStackTrace();
	}
	return false;

    }

    @Override
    public boolean saveMangerRole(SysManager bManager) throws OutMsgException {
	SysManager ymManager = findByManagerId(bManager.getId());
	if (null != ymManager) {
	    ymManager.setRoles(bManager.getRoles());
	    sysManagerDao.update(ymManager);
	}
	return true;
    }

}
